﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using V2TEK.NET.SH.Silverlight.Charts.GanttHelper;
using System.Linq;
using System.Collections.ObjectModel;
using V2TEK.NET.SH.Silverlight.Charts.Core.Schedule;

namespace V2TEK.NET.SH.Silverlight.GanttView
{
    public class GeneralPropertyDependecy
    {
        public void Link(ITaskDescriptor task)
        {
            task.PropertyChanged += task_PropertyChanged;

            task.Children.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Children_CollectionChanged);
        }

        void Children_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count > 0)
            {
                ITaskDescriptor task = e.NewItems[0] as ITaskDescriptor;
                UpdateTask(task);
            }
            if (e.OldItems != null && e.OldItems.Count > 0)
            {
                ITaskDescriptor task = e.OldItems[0] as ITaskDescriptor;
                UpdateTask(task);
            }
        }

        public void UpdateTask(ITaskDescriptor task)
        {
            if (task.Parent != null)
            {
                DateTimeChanged(task);
                TotalMillisecondsChanged(task);
                PercentCompleteChanged(task);
            }
        }

        void DateTimeChanged(ITaskDescriptor task)
        {
            //设置起始或结束时间，改变工时
            if (task.Children.Count == 0)
            {
                if (task.HasStartDate && task.HasEndDate)
                {
                    double temp = ((ITime1)task).Calendar.GetWorkTime(task.StartDate, task.EndDate).TotalMilliseconds;
                    if (task.TotalMilliseconds != temp)
                        task.TotalMilliseconds = temp;
                }
            }

            if (task.Parent != null)
            {
                var result = from c in task.Parent.Children where c.HasEndDate select c.EndDate;
                if (result.Count() > 0)
                {
                    var max = result.Max();
                    if (!task.Parent.HasEndDate || task.Parent.EndDate < max)
                    {
                        //if (task.Parent.EndDate < max)
                        task.Parent.EndDate = max;
                    }
                }
                result = from c in task.Parent.Children where c.HasStartDate select c.StartDate;
                if (result.Count() > 0)
                {
                    var min = result.Min();
                    //if (task.Parent.StartDate > min)
                    if (!task.Parent.HasStartDate || task.Parent.StartDate > min)
                    {
                        task.Parent.StartDate = min;
                    }
                }
            }
        }

        void TotalMillisecondsChanged(ITaskDescriptor task)
        {
            ////设置工时，将改变开始日期
            //if (task.HasStartDate && task.HasEndDate)
            //{
            //    double millisencond = task.EndDate.Ticks - task.TotalMilliseconds;
            //    if (millisencond > DateTime.MinValue.Ticks && millisencond < DateTime.MaxValue.Ticks)
            //    {
            //        DateTime temp1 = task.EndDate - TimeSpan.FromMilliseconds(task.TotalMilliseconds) + TimeSpan.FromDays(1);

            //        if (task.StartDate != temp1)
            //            task.StartDate = temp1;



            //    }
            //}                   
            if (task.Parent != null)
            {
                double total = 0;
                foreach (var item in task.Parent.Children)
                {
                    total = total + item.TotalMilliseconds;
                }
                task.Parent.TotalMilliseconds = total;
            }
        }
        void PercentCompleteChanged(ITaskDescriptor task)
        {
            if (task.Parent != null)
            {
                double total = 0, percent = 0;
                foreach (var item in task.Parent.Children)
                {
                    total = total + item.TotalMilliseconds;
                    percent = percent + item.TotalMilliseconds * item.PercentComplete * 0.01;
                }
                if (total > 0)
                    task.Parent.PercentComplete = (float)(percent * 100 / total);
            }
        }

        void task_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SimpleTaskDescriptor task = sender as SimpleTaskDescriptor;
            switch (e.PropertyName)
            {
                case AbstractTaskDescriptor.STARTDATE_PROPERTY:
                case AbstractTaskDescriptor.ENDDATE_PROPERTY:
                    DateTimeChanged(task);
                    break;
                case AbstractTaskDescriptor.TOTALMILLISECONDS_PROPERTY:
                    TotalMillisecondsChanged(task);
                    break;
                case AbstractTaskDescriptor.COMPLETE_PERCENT_PROPERTY:
                    PercentCompleteChanged(task);
                    break;
            }
        }
    }
}
